var reservationId="";
function getValue(resId)
{
	reservationId = resId;
}

function CloseReasonWarning(id)
{
	 if(id == "reason-insert-close"|id=="insert-reason-btn-close")
	{
		$("#dialog-show-insert-reson").css("display","none");
	}
	 if(id=="confirm-email-close"|id=="reson-warning-btn-cancel")
	{
		$("#dialog-confirm-email").css("display","none");
	}
	 if(id=="sendemail-warning-close"|id=="sendemail-warning-btn-close")
		{
			$("#dialog-show-confirm-email").css("display","none");
			var checkResered =$("[id*=tappage_roommanage_menu][class = select]").attr("groupider");
			var reseredTitle =$("[id*=tappage_roommanage_menu][class = select]").attr("Title");
			if(checkResered == "0" || checkResered == "")
			{
				CloseTapPage(reseredTitle);
				//$("#edit_none_groups_"+reseredTitle).hide();
				//$("#tappage_roommanage_menu_"+reseredTitle).remove();					
			}
			else
			{
			var checkresLength = $("#resvList").find($("tr")).children().find($("[id*=edit_res_detail_atcount]")).not($("[id*=edit_res_detail_atcount]").last()).length;
				if(checkresLength > 1)
				{
					CloseTapPage(reseredTitle);
					//$("#edit_none_groups_"+reseredTitle).hide();
					//$("#tappage_roommanage_menu_"+reseredTitle).remove();					
				}
	
			}
		}
}

function ShowInsertTime()
{
$("#reason-insert-time").css("display","");	
if($("#insert-reson-timeH > option").length == 0){
for ( var i = 0; i < 24 ; i++) {
	if(i<10)
	{
		$("#insert-reson-timeH").append( $("<option>").val("0"+i).html("0"+i));
		
	}
	if(i >=10)
	{
		$("#insert-reson-timeH").append( $("<option>").val(i).html(i));
		
	}
}
}

if($("#insert-reson-timeM > option").length == 0){
	for ( var i = 0; i < 60 ; i++) {
		if(i<10)
		{
			$("#insert-reson-timeM").append( $("<option>").val("0"+i).html("0"+i));
			
		}
		if(i >=10)
		{
			$("#insert-reson-timeM").append( $("<option>").val(i).html(i));
			
		}
	}
	}

}
function ShowInsertEmail()
{
	
	if ($('#insert-reason-send-email').is(':checked')) {
	    $("#insert-massage").css("display","");
	} else {
	    $("#insert-massage").css("display","none");
	    $("#reason-insert-email-massage").css("display","none");
	    $("#dialog-confirm-email").css("display","none");
	    
	} 
}

function ShowMassageBox()
{
	$("#reason-insert-email-massage").css("display","");
}

function AddReasonCancel()
{
	
	if($("#insert-reson-cost").val() == ""){
		alert('please enter charge amount');
		return false;
	}
	CloseReasonWarning('insert-reason-btn-close');
	if($("#chooseRoomCancel").val()=='chooseRoomCancel')
	{
		btnCancelCheckInGroupAgain();
	}
	if ($('#insert-reason-send-email').is(':checked')) {
		
	    $("#dialog-confirm-email").css("display","");
		$( "#dialog-confirm-email" ).offset({top:480,left:550});
		$( "#dialog-confirm-email" ).css("z-index",100002);
		$("#dialog-confirm-email").css("display","");
		$("#dialog-confirm-email").css("position","fixed");
		$("#dialog-confirm-email").css("box-shadow","1px 4px 10px #000");
		$("#dialog-confirm-email").css("background-color","#FFF");
		$("#dialog-confirm-email").draggable();
	}
	var StringArray  = new Array();
	var checkResered =$("[id*=tappage_roommanage_menu][class = select]").attr("groupider");
	if(checkResered != "0"&&checkResered!="")
	{
		$("input[name*=edit_res_detail_at]:checked").each(function() {
			if($(this).attr("reserved")!=""&&$(this).attr("reserved")!=undefined){
				StringArray.push($(this).attr("reserved"));
			}
		});	
	}else
	{
		StringArray.push(reservationId);
	}
	if(StringArray==""){
		StringArray.push($($("#insert-reson-cost"))[0].resid);
		//$($("#insert-reson-cost"))[0].resid = 0;
	}
	var reason = $( "#insert-reson-reason" ).val();
	var detail =$("#insert-reson-resondetail").val();
	var cost =$("#insert-reson-cost").val();
	
	var date = new Date();
	if($("#insert-reson-timeH").val() == null)
	{
		var timeH = "00";
	}else
	{
		var timeH =$("#insert-reson-timeH").val();
	}
	
	if($("#insert-reson-timeH").val() == null)
	{
		var timeM = "00";
	}else
	{
		var timeM =$("#insert-reson-timeM").val();
	}
	var dateTime =date.toFormatString('yyyy-MM-dd');
	dateTime +=" "+timeH+":"+timeM;
	var Massage=$("#insert-email-massage").val();
	var linkAddReason ="/reasonDetailService?func=addReasonDetail&resId="+StringArray+"&reason="+reason+"&detail="+detail+"&cost="+cost+"&dateTime="+dateTime+"&massage="+Massage;
	$.ajax({
		// Call
		url : linkAddReason,
		type : "get",
		data : {
			format : "json"
		},
		datatype : "json",
		timeout : 2000,
		// if Call Success //
		success : function(data) {
			var objArray = new Array();
			objArray = JSON.parse(data);
			cancelReservationGroupAndSingle(cost,objArray.id);
			if(!$('#insert-reason-send-email').is(':checked'))
			{
			var reseredTitle =$("[id*=tappage_roommanage_menu][class = select]").attr("Title");
			if(checkResered == "0" || checkResered == "")
			{
				CloseTapPage(reseredTitle);
				//$("#edit_none_groups_"+reseredTitle).hide();
				//$("#tappage_roommanage_menu_"+reseredTitle).remove();					
			}
			else
			{
			var checkresLength = $("#resvList").find($("tr")).children().find($("[id*=edit_res_detail_atcount]")).not($("[id*=edit_res_detail_atcount]").last()).length;
				if(checkresLength > 1)
				{
					CloseTapPage(reseredTitle);
					//$("#edit_none_groups_"+reseredTitle).hide();
					//$("#tappage_roommanage_menu_"+reseredTitle).remove();					
				}
	
			}
			
			}
			
		},

	});
}
function cancelReservationGroupAndSingle(cost,id){
	if($("#chooseRoomCancel").val()!="chooseRoomCancel")
	{
		//for single
		var sellRoom_editAfRes = false;
		var isAss_editAfRes = false;
		var	roomStatus_editAfRes = "Cancel Reservation";
		var nowDate = new Date();
		mkMonth=nowDate.getMonth()+1;
		mkMonth=new String(mkMonth);
		if(mkMonth.length==1){
			mkMonth="0"+mkMonth;
		}
		mkDay=nowDate.getDate();
		mkDay=new String(mkDay);
		if(mkDay.length==1){
			mkDay="0"+mkDay;
		}
		var Hours = nowDate.getHours();
		var Minutes = nowDate.getMinutes();
		nowDate = nowDate.getFullYear() +"-"+ mkMonth +"-"+ mkDay +" "+Hours+":"+Minutes;
		
		reservationId = getCurrentTab().attr('refid');
		if(reservationId==undefined){
			reservationId = $($("#insert-reson-cost"))[0].resid;
		}
		var changeStatusLink = "/reservationService?func=editAfterRes&resId=" +reservationId+"&reservationstatus="+roomStatus_editAfRes+"&sellroom="+sellRoom_editAfRes+"&isassign="+isAss_editAfRes+"&dateCheckin="+nowDate;
		$.ajax({
			// Call
			url : changeStatusLink,
			type : "get",
			data : { format : "json" },
			datatype : "json",
			timeout:5000,
			success : function(data) {
				setTimeout(function(){showNumberall_reservation();},50);
				var DateCharge = new Date();
				DateCharge = (DateCharge.getFullYear() + "-" + (DateCharge.getMonth() + 1) + "-" + DateCharge.getDate() + " " + DateCharge.getHours() + ":" + DateCharge.getMinutes());
				var url = "/chargeService?func=addCharge&description=" + 'Cancel Reservation' + "&chargeQty=" + 1 + "&chargeAmount=" + cost + "&chargeDate=" + DateCharge + "&discount=" + '0' + "&tax=" + '0' + "&isRoomCharge=true";
				addChargeCancelReservation(url,reservationId);
				
				
				$("[name = edit_groups]").css("display","none");
				$("[name=edit_none_groups]").css("display","none");
				CloseTapPage($("[id*=tappage_roommanage_menu_editreserved][class=select]").attr("title"));
				cerltimespan($(".timespanbuttonactive").get(0));
				CloseAllPopup("editres-deleteroom-close");
				if(objArray == null){
					CheckError();
				}
				//;
			}
		});
	}else{
		//for Group
		btnCancelCheckInAllGroup(cost,id);//pao
	}
}
function CloseReasonCancelDialog()
{
	$("#dialog-show-insert-reson").css("display",'none');
}

function LoadResonDropbox()
{
	var link ="/reasonCancelService?func=loadReasonCancel" ;
	$.ajax({
		// Call
		url : link,
		type : "get",
		data : {
			format : "json"
		},
		datatype : "json",
		timeout : 2000,

		// if Call Success //
		success : function(data) {
			var objArray = new Array();
			objArray = JSON.parse(data);
			var opts = objArray.length;
			if($("#insert-reson-reason > option").length == 0){
			for ( var i = 0; i < opts; i++) {
				$("#insert-reson-reason").append( $("<option>").val(objArray[i].id).html(objArray[i].reason));
				}
			}
		},

	});	
}

function SendEmail()
{
	if($("#insert-confirm-email").val() == null || $("#insert-confirm-email").val() =="")
	{
		$("#dialog-confirm-email").css("display","none");	
	}
	else
	{
		
		$("#dialog-show-confirm-email").css("display","");	
		 $("#dialog-confirm-email").css("display","none");
		 $("#dialog-show-insert-reson").css("display","none");
		 
			$( "#dialog-show-confirm-email" ).offset({top:480,left:550});
			$( "#dialog-show-confirm-email" ).css("z-index",100001);
			$("#dialog-show-confirm-email").css("position","fixed");
			$("#dialog-show-confirm-email").css("box-shadow","1px 4px 10px #000");
			$("#dialog-show-confirm-email").css("background-color","#FFF");
			$("#dialog-show-confirm-email").draggable();
	}

}